<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8" />
    <title>告警修复</title>
    <script th:src="@{/js/jquery.js}"></script>

</head>
<body>

<form name="alarmInfo">
	<table width="100%">
		<tr>
    		<td align="center"><h1>告警修复</h1></td>
    	</tr>
    </table>
    <table align="center">
        <tr>
            <td>广场:</td>
            <td>
            	<input id="plazaName" type="text" name="plazaName" style="width: 220px" />
            </td>
            <td>&emsp;&emsp;</td>
            <td>告警类型:</td>
            <td>
            	<select id="alarmType" type="text">
            		<option value ="">全部</option>
            	</select>
            </td>
            <td>&emsp;&emsp;</td>
            <td>开始时间:</td>
            <td>
            	<input id="startTime" type="date" name="startTime" style="width: 130px" />
            </td>
            <td>&emsp;&emsp;</td>
            <td>结束时间:</td>
            <td>
            	<input id="endTime" type="date" name="endTime" style="width: 130px" />
            </td>
            <td>&emsp;&emsp;</td>
            <td><input type="button" value="查询" onclick="query()" /></td>
            <td>&emsp;&emsp;</td>
            <td><input type="button" value="批量恢复" onclick="BatchRepair()" /></td>
        </tr>
    </table>
	<div>
    	<hr>
	</div>
    <div id="result">
    </div>
</form>
</body>


<script type="text/javascript">
	function query(){
		var plazaName = alarmInfo.plazaName.value;
        var alarmTypeId = alarmInfo.alarmType.value;
        var startTime = alarmInfo.startTime.value;
        var endTime = alarmInfo.endTime.value;
        if(startTime == '' || endTime == ''){
        	alert('开始时间和结束时间必填！');
        	return false;
        }
        if(startTime > endTime){
        	alert('开始时间不能大于结束时间！');
      		return false;
        }
		
        $.ajax({
			type : "POST",
			url : "/getAlarmList",
			data : JSON.stringify({
				"plazaName": $.trim(plazaName),
				"alarmTypeId": alarmTypeId,
				"startTime": startTime +' 00:00:00',
				"endTime": endTime +' 23:59:59'
				}),
			contentType : "application/json",
			success : function(result) {
				if (result.errcode == 200) {
					$("#result").empty();
					var html = '';
					if(result.data!=null && result.data.length>0){
						html="<table align='center' border='1' cellspacing='0'><tr>"
						html+="<td align='center' width='1%'><input type='checkbox' id='selectAll'></td>"
						html+="<td align='center'><h4>广场</h4></td>"
						html+="<td align='center'><h4>品牌</h4></td>"
						html+="<td align='center' width='6%'><h4>铺位号</h4></td>"
						html+="<td align='center' width='6%'><h4>电表名称</h4></td>"
						html+="<td align='center' width='5%'><h4>电表编号</h4></td>"
						html+="<td align='center'><h4>告警项</h4></td>"
						html+="<td align='center'><h4>告警内容</h4></td>"
						html+="<td align='center' width='5%'><h4><font color='red'>当前状态</font></h4></td>"
						html+="<td align='center' width='7%'><h4>告警开始时间</h4></td>"
						html+="<td align='center' width='4%'><h4>操作</h4></td>"
						for (var i=0;i<result.data.length;i++){
		                	var item = result.data[i];
		                	html+="<tr><td><input type='checkbox' name='selectAlarm' value="+item.alarmId+"></td>"
							html+="<td>"+item.plazaName+"</td>"
							html+="<td>"+item.brandName+"</td>"
							html+="<td>"+item.bunkName+"</td>"
							html+="<td>"+item.deviceName+"</td>"
							html+="<td>"+item.deviceNo+"</td>"
							html+="<td>"+item.alarmTypeName+"</td>"
							html+="<td>"+item.alarmContent+"</td>"
							html+="<td>"+item.status+"</td>"
							html+="<td>"+item.alarmTimeStr+"</td>"
							html += "<td><input type=\"button\" value=\"恢复告警\" onclick=\"repair('"
                                +item.alarmId
                                +"')\"/></td>"
							html+="</tr>"
						}
						html+="</table>"
						$("#result").append(html);
						init();
						checkList = [];//初始化选择列表
					}
					else{
						html = '<h3>暂无告警记录！！！</h3>';
						$("#result").append(html);
	                }
					
				} else {
					alert("系统异常，联系管理员！");
				}
			}
		});
	}
		
	function repair(alarmId){
		//alert(alarmId);
		$.post("/repairAlarm",
			{"alarmId":alarmId},
			function(data){
				if(data.errcode == 200){
            		alert(data.errmsg);
            		query();
            	}
            	else{
            		alert("修复失败，请联系管理员！");
            	}
		});
	}	
	
	function BatchRepair(){
		if(checkList == null || checkList.length == 0){
			alert("至少勾选一条记录！");
			return false;
		}
		else{
			if (confirm("是否恢复选中告警!")) {
				//alert(checkList);
				$.ajax({
					type : "POST",
					url : "/batchRepairAlarm",
					data : JSON.stringify(checkList),
					contentType : "application/json",
					success : function(data) {
						if (data.errcode == 200) {
							alert(data.errmsg);
							query();
						} else {
							alert("修复失败，请联系管理员！");
						}
					},
	                error:function (data) {           //失败回调
	                   	alert("系统异常，联系管理员！");
	                }
	            }); 
			} else {
			}
		}	
	}
	
	$(function getAlarmTypeList() {
	    $.ajax({
	        type: "POST",
	        url: '/getAlarmTypeList',
	        contentType: "application/json",
	        datatype: "json",
	        success: function (result) {
	        	//根据id查找对象，
	        	var obj=document.getElementById('alarmType');
	        	//循环添加选项
	        	for(var i in result){ 
	        		obj.add(new Option(result[i].alarmTypeName,result[i].alarmTypeId)); 
	        	}
	        }
	    });
	})
	
	var checkList = [];// 定义全局alarmId列表
	function init(){
        var selectAll=document.getElementById("selectAll");
        var alarmList=document.getElementsByName("selectAlarm");
        //全选/全不选
        selectAll.onchange=function() {
        	checkList = [];
            if(this.checked) {
                for(var i=0;i<alarmList.length;i++) {
                    alarmList[i].checked=true;
                    checkList.push(alarmList[i].value);
                }
            }else {
                for(var i=0;i<alarmList.length;i++) {
                    alarmList[i].checked=false;
                }
            }
        }
      	//单个点击复选框
        for(var i=0;i<alarmList.length;i++) {
            alarmList[i].onchange=function() {
                if(this.checked) {
                	checkList.push(this.value);
                }else {
                	const index = checkList.indexOf(this.value);
                	checkList.splice(index, 1);
                }
            }
        }
    }
     
</script>
</html>